<template>
    <div>
        <Card shadow>
            <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
            </iview-table>
            <br>
            <iview-page 
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber" 
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
            </iview-page>
        </Card>
        <Modal v-model="showAdd" width="700" @on-cancel="cancelModel">
            <h3 slot="header" style="color:#2D8CF0">请填写以下信息</h3>
            <Row>
                <Col span="22" offset="1">
                    <Card :bordered="false" :dis-hover="true">
                        <p slot="title" style="height: 30px">
                            投诉人：<Tag v-if="formItem.patientName !== ''" color="blue">{{formItem.patientName}}</Tag>
                        </p>
                        <Input v-model="searchValue" placeholder="请通过病历号/手机号码检索" @keyup.enter.native="search"/>
                        <p style="color: #ff9900"><Icon type="information-circled"></Icon> 按回车键检索</p>
                    </Card>
                    <Card :bordered="false" :dis-hover="true">
                        <p slot="title" style="height: 33px">
                            投诉详情
                        </p>
                        <Form ref="formItem" :model="formItem" :label-width="100">
                            <FormItem label="相关人员：">
                                <Select v-model="relatedPersonnel" clearable transfer multiple>
                                    <Option v-for="item in users" v-if="item.isAdmin !== 1" :value="item.id" :key="item.id">{{ item.username }}</Option>
                                </Select>
                            </FormItem>
                            <FormItem label="投诉级别：">
                                <Select v-model="formItem.level" clearable transfer>
                                    <Option value="0" label="I级">
                                        <span>I级</span>
                                        <span style="float:right;color:#ccc">一般性投诉和抱怨，由客服经理接待解决</span>
                                    </Option>
                                    <Option value="1" label="II级">
                                        <span>II级</span>
                                        <span style="float:right;color:#ccc">一般性投诉，但是需要退费，由经理接待解决</span>
                                    </Option>
                                    <Option value="2" label="II级">
                                        <span>II级</span>
                                        <span style="float:right;color:#ccc">较严重的投诉，设计赔偿等，由总经理接待解决</span>
                                    </Option>
                                    <Option value="3" label="IV级">
                                        <span>IV级</span>
                                        <span style="float:right;color:#ccc">严重投诉，设计司法诉讼或者行政诉讼，由总经理接待解决</span>
                                    </Option>
                                </Select>
                            </FormItem>
                            <FormItem label="投诉方式：">
                                <Select v-model="formItem.way" clearable transfer>
                                    <Option value="0">电话</Option>
                                    <Option value="1">网络</Option>
                                    <Option value="2">到店</Option>
                                </Select>
                            </FormItem>
                            <FormItem label="事件对象：">
                                <Select v-model="formItem.reasonId" clearable transfer :label-in-value=true @on-change="changeReason">
                                    <Option v-for="item in reasons" :value="item.id" :key="item.id">{{ item.reason }}</Option>
                                </Select>
                            </FormItem>
                            <FormItem label="事件经过：">
                                <Input v-model="formItem.process" :rows="5" :autosize="{minRows: 5}" type="textarea" placeholder=""/>
                            </FormItem>
                            <FormItem label="转交部门：">
                                <Select v-model="formItem.departmentId" clearable transfer :label-in-value=true @on-change="changeDepartment">
                                    <Option v-for="item in departments" :value="item.id" :key="item.id">{{ item.name }}</Option>
                                </Select>
                            </FormItem>
                            <FormItem label="接待人员：">
                                <Select v-model="formItem.userId" clearable transfer :label-in-value=true @on-change="changeUser">
                                    <Option v-for="item in receptions" :value="item.id" :key="item.id">{{ item.username }}</Option>
                                </Select>
                            </FormItem>
                        </Form>
                    </Card>
                </Col>
            </Row>
            <div slot="footer">
                <Button type="info" size="large" @click="save">保存</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
export default {
    components: {
        iviewTable,
        iviewPage
    },
    data () {
        return {
            searchValue: '',
            showAdd: false,
            baseURL: '/user/reason/complaint',
            patientURL: '/user/patient',
            selectRows: [],
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/reason/complaint',
                defaultFilters: [
                ]
            },
            dataModel: {
                menuId: 1715,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '患者名称',
                        key: 'patientName'
                    },{
                        title: '病历号',
                        key: 'medicalNumber'
                    },{
                        title: '相关人员',
                        key: 'relatedPersonnelName'
                    },{
                        title: '投诉级别',
                        key: 'level',
                        render: (h, params) => {
                            let temp = ''
                            if (params.row.level === 0) {
                                temp += 'I级'
                            } else if (params.row.level === 1) {
                                temp += 'II级'
                            } else if (params.row.level === 2) {
                                temp += 'II级'
                            } else if (params.row.level === 3) {
                                temp += 'IV级'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '投诉方式',
                        key: 'way',
                        render: (h, params) => {
                            let temp = ''
                            if (params.row.way === 0) {
                                temp += '电话'
                            } else if (params.row.way === 1) {
                                temp += '网络'
                            } else if (params.row.way === 2) {
                                temp += '到店'
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '事件对象',
                        key: 'reasonName'
                    },{
                        title: '事件经过',
                        key: 'process'
                    },{
                        title: '部门名称',
                        key: 'departmentName'
                    },{
                        title: '接待人员',
                        key: 'userName'
                    },{
                        title: '投诉时间',
                        key: 'creationTime',
                        render: (h, params) => {
                            return h('span', this.$moment(params.row.creationTime).format('YYYY年MM月DD日'))
                        }

                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            },
            users: [],
            relatedPersonnel: [],
            reasons: [],
            departments: [],
            receptions: [],
            formItem: {
                id: '',
                patientId: '',
                patientName: '',
                medicalNumber: '',
                relatedPersonnel: '',
                level: '',
                way: '',
                reasonId: '',
                reasonName: '',
                process: '',
                departmentId: '',
                departmentName: '',
                userId: '',
                userName: ''
            }
        };
    },
    methods: {
        loadUser () {
            let url = '/user/findAll'
            this.$ajax.get(url).then(r => {
                let {code, data, msg} = r.data
                if (code === 0) {
                    this.users = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        loadReason () {
            let url = '/user/complaints/reason/findAll'
            this.$ajax.get(url).then(r => {
                let {code, data, msg} = r.data
                if (code === 0) {
                    this.reasons = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        loadDepartment () {
            let url = '/user/department/findAll'
            this.$ajax.get(url).then(r => {
                let {code, data, msg} = r.data
                if (code === 0) {
                    this.departments = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        search () {
            if (this.searchValue !== '') {
                let url = this.patientURL + '/search'
                this.$ajax.post(url, this.addFilter(this.searchValue, 'phone', 'medical_number')).then(response => {
                    let {code, data} = response.data
                    if (code === 0) {
                        if (data.length > 0) {
                            if (data.length === 1) {
                                this.formItem.patientId = data[0].id
                                this.formItem.patientName = data[0].name
                                this.formItem.medicalNumber = data[0].medicalNumber
                            } else {
                                this.$bus.modal('warning', '系统提示', '查询异常,检索到多个患者信')
                            }
                        } else {
                            this.$bus.modal('warning', '系统提示', '未查询到患者信息')
                        }
                    }
                })
            } else {
                this.$bus.modal('warning', '系统提示', '请输入检索关键词')
            }
        },
        addFilter (val, ...fields) {
            let queryModel = {
                mode: 'or',
                pageNumber: 1,
                pageSize: 10,
                order: 'id',
                filters: []
            }
            for (let field of fields) {
                let filterModel = {'field': field, 'value1': val, 'operate': 'LIKE'}
                queryModel.filters.push(filterModel)
            }
            return queryModel
        },
        events (item) {
            let path = item.path
            if (path === 'add') {
                this.add()
            } else if (path === 'delete') {
                this.delete()
            }
        },
        add () {
            // 加载用户
            this.loadUser()
            // 加载投诉原因
            this.loadReason()
            // 加载部门
            this.loadDepartment()
            this.searchValue = ''
            this.relatedPersonnel = []
            this.receptions = []
            this.$bus.init(this.formItem)
            this.showAdd = true
        },
        save () {
            if (this.formItem.patientId !== '') {
                this.formItem.relatedPersonnel = JSON.stringify(this.relatedPersonnel)
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '保存成功')
                        this.showAdd = false
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            } else {
                this.$bus.alert('warning', '请选择投诉人')
            }
        },
        delete () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                if (ids.length === 1) {
                    this.doDelete(ids)
                } else {
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除'+ids.length+'条记录?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                }
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doDelete (ids) {
            let url = this.baseURL + '/delete'
            this.$ajax.post(url, ids).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', msg)
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            });
        },
        refresh () {
            this.$refs.table.refresh()
        },
        changeReason (option) {
            this.formItem.reasonId = option.value
            this.formItem.reasonName = option.label
        },
        changeDepartment (option) {
            this.formItem.departmentId = option.value
            this.formItem.departmentName = option.label
            let url = '/user/findByDepartmentId/' + option.value
            this.$ajax1.get(url).then(r => {
                let {code, data, msg} = r.data
                if (code === 0) {
                    this.receptions = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        changeUser (option) {
            this.formItem.userId = option.value
            this.formItem.userName = option.label
        },
        changeSelect (rows) {
            this.selectRows = rows
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        init () {
            this.refresh()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/complaint') {
                this.init()
            }
        }
    }
}
</script>

<style>

</style>
